package com.bizihang.validate.code.core.support;

import com.bizihang.validate.code.core.Client;
import com.bizihang.validate.code.core.ValidateCode;
import com.bizihang.validate.code.core.ValidateCodeGenerator;
import com.bizihang.validate.code.core.support.generator.ImageCodeGenerator;
import com.bizihang.validate.code.core.support.generator.TextCodeGenerator;

import static com.bizihang.validate.code.core.ValidateCode.TYPE;

/**
 * 验证码生成策略
 */
public class CodeGeneratorStrategy {
    private ValidateCodeGenerator generator;

    public CodeGeneratorStrategy(TYPE codeType) {
        /**
         * 根据客户端的条件生成generator
         */
        switch (codeType) {
            case IMAGE:
                generator = new ImageCodeGenerator();
                break;
            case SMS:
            case EMAIL:
                generator = new TextCodeGenerator();
                break;
            default:
                throw new RuntimeException("无法找到正确的验证码生成器");
        }
    }

    public ValidateCode generate(Client request) {
        return generator.generate(request);
    }
}
